<?php
namespace App\Controller;
use Common\Controller\AppbaseController;
use Think\Controller;

class BusinesspublicController extends AppbaseController {
    //用户获取短信
	public function getPhoneCode(){
        $phone=I('phone');
        $type=I('type',0,'intval');
        if(!$phone||!$type){
            $this->jsonReturn(0,'电话必填');
        }
        if(!lt_is_telephone($phone)){
            $this->jsonReturn(0,'电话格式不正确');
        }
        $user=M('Business')->where(['phone'=>$phone])->field('id,is_prepared')->find();
        switch ($type){
            //注册需要手机号不存在
            case 1 :
                if($user&&($user['is_prepared']==1)){
                    $this->jsonReturn(0,'手机号已存在');
                }
                break;
            //更换手机号需要手机号存在
            case 2 :
                if(!$user){
                    $this->jsonReturn(0,'手机号不存在');
                }
                break;
            default :
                $this->jsonReturn(0,'非法数值');
        }
        $this->sendSmsAndLog($phone);
	}

    /**
     * 商家上传接口
     */
    public function businessUploadImage(){
        $token=I('token');
        $uuid=I('uuid');
        if(!$token && !$uuid){
            $this->jsonReturn(0,'缺少参数');
        }
        if($token){
            $business=lt_get_business_by_token($token);
        }else{
            $business=M('Business')->where(['uuid'=>$uuid])->find();
        }
        if(!$business){
            $this->jsonReturn(0,'请先登录');
        }
        if($business['status']==0){
            $this->jsonReturn(0,'用户已被拉黑');
        }
        $result=$this->uploadFile('business');
        if($result['status']!=='error'){
            $fileName=$result['info']['file']['savepath'].$result['info']['file']['savename'];
            $this->jsonUnslashesReturn(1,'上传成功',$this->uploadPre.$fileName);
        }else{
            $this->jsonReturn(0,$result['message']);
        }
    }

    //上传方法
    protected function uploadFile($savepath){
        $config=array(
            'rootPath' => './'.C("UPLOADPATH"),
            'savePath' => $savepath.'/'.date('Ymd').'/',
            'maxSize' => 5242880,//5m
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    ['jpg','png','jpeg','gif','bmp'],
            'autoSub'    =>    false,
        );
        $upload = new \Think\Upload($config);
        $info=$upload->upload();
        if($info){
            return ['status'=>'success','info'=>$info];
        }else{
            return ['status'=>'error','message'=>$upload->getError()];
        }
    }

    /**
     * 获取省份
     */
    public function getProvinces(){
        $provinces=M('Region')->where(['pid'=>0])->field('id,name')->select();
        if($provinces){
            $this->jsonReturn(1,'获取成功',$provinces);
        }else{
            $this->jsonReturn(0,'获取失败');
        }
    }

    /**
     * 根据省份id获取城市
     */
    public function getCitiesByProvinceId(){
        $id=I('id');
        $cities=M('Region')->where(['pid'=>$id])->field('id,name')->select();
        if($cities){
            $this->jsonReturn(1,'获取成功',$cities);
        }else{
            $this->jsonReturn(0,'获取失败');
        }
    }

    /**
     * 根据城市id获取区域
     */
    public function getDistrictsByCityId(){
        $id=I('id');
        $districts=M('Region')->where(['pid'=>$id])->field('id,name')->select();
        if($districts){
            $this->jsonReturn(1,'获取成功',$districts);
        }else{
            $this->jsonReturn(0,'获取失败');
        }
    }

    //获取商户状态
    public function getBusinessStatus(){
        $token=I('token');
        if(!$token){
            $this->jsonReturn(0,'缺少参数');
        }
        $business=lt_get_business_by_token($token);
        if(!$business){
            $this->jsonReturn(0,'商户不存在');
        }
        $this->jsonReturn(1,'获取成功',['status'=>$business['status'],'is_checked'=>$business['is_checked']]);
    }
}